package com.kws.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kws.payment.business.WeixinWebPayBusiness;
import com.kws.payment.dao.CreateWeixinPayNotifyRq;
import com.kws.payment.dao.CreateWeixinPayOrderRq;
import com.kws.payment.dao.WeixinPayRefundRq;
import com.kws.payment.service.WeixinWebPayService;
import com.wechat.pay.contrib.apache.httpclient.util.AesUtil;
import com.wechat.pay.java.service.refund.model.Refund;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.HashMap;

/**
 * <b>WeixinWebPayServiceImpl</b>
 *
 * @description: WeixinWebPayServiceImpl <br>
 * @date: 2024/11/13 15:31 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class WeixinWebPayServiceImpl implements WeixinWebPayService {
    private static final Logger logger = LoggerFactory.getLogger(WeixinWebPayServiceImpl.class);

    @Autowired
    private WeixinWebPayBusiness weixinWebPayBusiness;


    @Override
    public String createWeixinWebPayOrder(CreateWeixinPayOrderRq rq) {
        return weixinWebPayBusiness.createWeixinWebPayOrder(rq);
    }

    @Override
    public void closeWeixinWebPayOrder(String orderNo) {
        weixinWebPayBusiness.closeWeixinWebPayOrder(orderNo);
    }

    @Override
    public Refund weixinPayRefundOrder(WeixinPayRefundRq rq) {
        return weixinWebPayBusiness.weixinPayRefund(rq);
    }

    @Override
    public boolean weixinNotify(String body) throws GeneralSecurityException {
        JSONObject jsonObject = JSON.parseObject(body);
        if ("TRANSACTION.SUCCESS".equals(jsonObject.get("event_type"))){
            logger.info("支付成功");
            CreateWeixinPayNotifyRq createWeixinPayNotifyRq = weixinWebPayBusiness.weixinNotify(jsonObject);
            return true;
        }else if ("REFUND.SUCCESS".equals(jsonObject.get("event_type"))){
            logger.info("退款成功");
            weixinWebPayBusiness.refundOrder(jsonObject);
            return true;
        }
        return false;
    }
}
